<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="utf-8">
    <base th:href="${#request.getContextPath()}+'/'">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>用户权限信息</title>

    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
    <![endif]-->

    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="js/jquery-3.1.1.js"></script>
    <!--<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>-->
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
    <script src="js/bootstrap-treeview.js"></script>
    <link href="css/bootstrap-treeview.css">
    <link href="css/default.css">
    <link href="css/normalize.css">
</head>
<body>

<div class="container-fluid">

    <div id="content">
        <div class="col-md-10">
            <div class="col-md-2">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">所有权限1</h3>
                    </div>
                    <div id="all_func" class="panel-body">
                        <button th:each="func : ${hasnosysModules}" class="btn btn-default" value="0" th:name="${func.module_Code}"
                                onclick="chooseFunc(this)" th:text="${func.module_Name}">商品添加</button>
                        <!--<button class="btn btn-default" value="0" onclick="chooseFunc(this)">商品列表</button>-->
                    </div>
                </div>
            </div>
            <div class="col-md-1">
                <button class="btn btn-default" onclick="addFunc()">添加</button> <br/>
                <button class="btn btn-default" onclick="delFunc()">移除</button>
            </div>
            <div class="col-md-2">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">拥有权限</h3>
                    </div>
                    <div id="has_func" class="panel-body">
                        <button th:each="func : ${hassysModules}" class="btn btn-default" th:name="${func.module_Code}" value="0"
                                onclick="chooseFunc(this)" th:text="${func.module_Name}">商品添加</button>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<script>



    function chooseFunc(item){
        var val = $(item).val();
        if (val == 0) {
            $(item).attr("class","btn btn-primary");
            var moduleCode = $(item).attr("name");
            $(item).val(moduleCode);
        } else {
            $(item).attr("class","btn btn-default");
            $(item).val(0);
        }

    }

    function addFunc(){
        var chooseChild = $("#all_func").children(".btn-primary");
        // $("#has_func").append(chooseChild);
        // var len = chooseChild.length;

        var moduleId =$(chooseChild[0]).val();
        // for(var i=0;i<len;i++){
        //     $(chooseChild[i]).attr("class","btn btn-default");
        //     $(chooseChild[i]).val(0);
        // }

        $.post("user/addfunc",{moduleId:moduleId,roleId:'57414897'},function (data) {
            if (data.resultCode==0) {
                var chooseChild = $("#all_func").children(".btn-primary");
                $("#has_func").append(chooseChild);
                var len = chooseChild.length;
                for(var i=0;i<len;i++){
                    $(chooseChild[i]).attr("class","btn btn-default");
                    $(chooseChild[i]).val(0);
                }
            }
        });
    }

    function delFunc(){
        var chooseChild = $("#has_func").children(".btn-primary");
        var len = chooseChild.length;
        var delCode = new Array();
        for(var i=0;i<len;i++){
            var oldCode = $(chooseChild[i]).val();
            delCode.push(oldCode)
        }

        console.log(delCode);
        $.post("user/delfunc",{moduleCode:delCode[0]},function (data) {
            if (data.resultCode==0) {
                var chooseChild = $("#has_func").children(".btn-primary");
                $("#all_func").append(chooseChild);
                var len = chooseChild.length;
                for(var i=0;i<len;i++){
                    var oldCode = $(chooseChild[i]).val();
                    $(chooseChild[i]).attr("class","btn btn-default");
                    $(chooseChild[i]).val(0);

                }
            }
        })
    }

</script>



</body>
</html>